package g4meThird;


import java.util.Random;
import java.awt.Dimension;
import java.lang.Math;
import java.util.ArrayList;

import Jeux.Joueur;
import Jeux.MiniJeux;
import Jeux.SimpleFont;

import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SavedState;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.TextField;

import t2s.SIVOXDevint;

public class WordBlaster extends MiniJeux implements ComponentListener {
	private int width;
	private SimpleFont font;
	private Words words;
	private ArrayList<Integer[]> coordonne = new ArrayList<Integer[]>();
	private ArrayList<Color> color=new ArrayList<Color>();
	private boolean gameOver = false;
	private int vitesse = 3;
	private int score = 0;
	private int multiplicateur = 1;
	private int time = 1;
	private int timeBeforeFreeze=0;
	private TextField boite;
	private SavedState state;
	private String resultat = "999";
	private int numberGoodResult = 1;
	private Sound laser;
	private int freeze = 1;
	private boolean freezeBool = false;
	private int bombe=0;
	private Animation fozen;
	private SIVOXDevint voix;
	private boolean aveugle=false;





	public WordBlaster(String title,Joueur joueur) {
		super(title);
		voix=new SIVOXDevint(3);
		Dimension tailleEcran = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		int hauteur = (int)tailleEcran.getHeight();
		int largeur = (int)tailleEcran.getWidth();
		this.joueur=joueur;
		words=new Words();
		try{
			app=new AppGameContainer(this,largeur,hauteur,false);
			app.setTargetFrameRate(1000);
			app.start();
		}catch(Exception e){
			System.out.println(e);
		}
		
	}

	@Override
	public void init(GameContainer container) throws SlickException {		
		Image[] img={
				new Image("../ressources/images/animationFrozen/1 (1).gif"),
				new Image("../ressources/images/animationFrozen/1 (2).gif"),
				new Image("../ressources/images/animationFrozen/1 (3).gif"),
				new Image("../ressources/images/animationFrozen/1 (4).gif"),
				new Image("../ressources/images/animationFrozen/1 (5).gif"),
				new Image("../ressources/images/animationFrozen/1 (6).gif"),
				new Image("../ressources/images/animationFrozen/1 (7).gif"),
				new Image("../ressources/images/animationFrozen/1 (8).gif"),
				new Image("../ressources/images/animationFrozen/1 (9).gif"),
				new Image("../ressources/images/animationFrozen/1 (10).gif"),
				new Image("../ressources/images/animationFrozen/1 (11).gif"),
				new Image("../ressources/images/animationFrozen/1 (12).gif"),
				new Image("../ressources/images/animationFrozen/1 (13).gif"),
				new Image("../ressources/images/animationFrozen/1 (14).gif"),
				new Image("../ressources/images/animationFrozen/1 (15).gif"),
				new Image("../ressources/images/animationFrozen/1 (16).gif"),
				new Image("../ressources/images/animationFrozen/1 (17).gif"),
				new Image("../ressources/images/animationFrozen/1 (18).gif"),
		};
		fozen=new Animation(img,200);
		font = new SimpleFont("Arial", 1, 70);
		width= container.getWidth();
		container.setShowFPS(false);
		laser =  new Sound("../ressources/sons/explosion.wav");
		boite = new TextField(container, font.get(), width/2-75, 720, 90, 80,this);
		state = new SavedState("Math");
		boite.setFocus(true);
		resultat = state.getString("a","999");	
		increaseCalcul();
	}

	@Override
	public void render(GameContainer container, Graphics graph) throws SlickException {
		if (freezeBool) {
			fozen.draw(70*width/100, 0);
		}
		texFieldFrame(graph);
		graph.drawLine(40*width/100, 0, 40*width/100, 999);
		graph.drawLine(68*width/100, 0, 68*width/100, 999);
		graph.drawImage(new Image("../ressources/images/neige.jpg"), 0, 675);
		graph.drawImage(new Image("../ressources/images/bombe.png"), 0, 620);
		font.get().drawString(50,675,"x"+freeze);
		font.get().drawString(50,620,"x"+bombe);
		graph.setFont(font.get());
		graph.setBackground(Color.black);
		showcalcul(container,graph);
		showscore(container,graph);
		showBarreCombo(container,graph);
		boite.render(container, graph);
		if(gameOver) font.get().drawString(width/2,container.getHeight()/2, ""+score+"!!!");

	}

	public void texFieldFrame(Graphics graph){
		graph.drawLine(width/2-75,720,width/2+75 , 720);
		graph.drawLine(width/2-75,800,width/2+75 , 800);
		graph.drawLine(width/2+75, 720, width/2+75, 800);

	}

	public void componentActivated(AbstractComponent source) {
		if (source == boite) {
			try {
				resultat = boite.getText();
				state.getString("a", resultat);
			} catch (NumberFormatException e) {
				if (boite.getText().equals("stop") && freeze > 0) {
					freezeBool=true;
					timeBeforeFreeze=time;
					freeze--;
				}
				else if (boite.getText().equals("boom") && bombe > 0) {
					words.clear();
					coordonne.clear();
					bombe--;
				}
				else{
					multiplicateur=1;
				}
			}
		}
		try {
			state.save();
			boite.setText("");

		} catch (Exception e) {

		}
	}

	@Override
	public void update(GameContainer container, int graph) throws SlickException {
		super.update(container, graph);
		stopFreeze();
		timeManage();
		if (joueur.getBlind()){
			if (time%(5500/vitesse) == 0) increaseCalcul();
		}else{
			if (time%(5500/vitesse) == 0) increaseCalcul();
		}
		goodResult( container,  graph);
		badResult();
		endGame(container);
		if (container.getInput().isKeyDown((Input.KEY_F4)) && words.size()!=0) {
			voix.playText(words.getCalcul(0));
		}
	}

	private void timeManage(){
		time++;
		if (time%(100/vitesse) == 0 && !freezeBool){
			for (int i=0;i< coordonne.size();i++){
				Integer[] j={coordonne.get(i)[0],coordonne.get(i)[1]+1};
				coordonne.set(i,j);
			}
		}

	}

	private void increaseCalcul() {
		if (!freezeBool){
			Random rand=new Random();
		
			if((words.size()+numberGoodResult)%3==0){
				color.add(Color.red);
			}else if((words.size()+numberGoodResult)%3==1){
				color.add(Color.white);
			}else{
				color.add(Color.yellow);
			}
			words.generateOneCal();
			if (joueur.getBlind()){
				int i=words.size()-1;
				String op;
				voix.playText(words.getCalcul(i));
			}
			double absi= rand.nextInt(60*width/100-40*width/100)+40*width/100;
			Integer[] j={(int) absi,0};
			coordonne.add(j);
		}
	}


	private void showcalcul(GameContainer container, Graphics graph) throws SlickException {
		if (words.size() != 0) {
			for (int i=0; i<words.size();i++){
				font.get().drawString(coordonne.get(i)[0],coordonne.get(i)[1],words.getCalcul(i),color.get(i));
			}
		}
	}

	private void showscore(GameContainer container, Graphics graph) throws SlickException{
		int y=740;
		int x=0;
		int realMul=(multiplicateur > 10)? 10 : multiplicateur;

		if (multiplicateur == 1){
			font.get().drawString(x,y,  "Score(x"+realMul+"):  "+score,Color.darkGray);

		}
		else if (multiplicateur == 2){
			font.get().drawString(x,y,  "Score(x"+realMul+"):  "+score,Color.gray);

		}
		else if (multiplicateur == 3){
			font.get().drawString(x,y,  "Score(x"+realMul+"):  "+score,Color.lightGray);
		}
		else if (multiplicateur == 4){
			font.get().drawString(x,y,  "Score(x"+realMul+"):  "+score);
		}
		else if (multiplicateur == 5){
			font.get().drawString(x,y,  "Score(x"+realMul+"):  "+score,new Color(153,255,255));
		}
		else if (multiplicateur == 5){
			font.get().drawString(x,y,  "Score(x"+realMul+"):  "+score,new Color(153,255,204));
		}
		else{
			font.get().drawString(x,y,  "Score(x"+realMul+"):  "+score,new Color(204,255,204));
		}
	}

	private void showBarreCombo(GameContainer container,Graphics graph) throws SlickException{
		if (multiplicateur == 1){
			graph.drawImage(new Image("../ressources/images/x1.jpg"), 10, 0);

		}
		else if (multiplicateur == 2){
			graph.drawImage(new Image("../ressources/images/x2.jpg"), 10, 0);
		}
		else if (multiplicateur == 3){
			graph.drawImage(new Image("../ressources/images/x3.jpg"), 10, 0);
		}
		else if (multiplicateur == 4){
			graph.drawImage(new Image("../ressources/images/x4.jpg"), 10, 0);
		}
		else if (multiplicateur == 5){
			graph.drawImage(new Image("../ressources/images/x5.jpg"), 10, 0);
		}
		else if (multiplicateur == 6){
			graph.drawImage(new Image("../ressources/images/x6.jpg"), 10, 0);
		}
		else if (multiplicateur == 7){
			graph.drawImage(new Image("../ressources/images/x7.jpg"), 10, 0);
		}
		else if (multiplicateur == 8){
			graph.drawImage(new Image("../ressources/images/x8.jpg"), 10, 0);
		}
		else if (multiplicateur == 9){
			graph.drawImage(new Image("../ressources/images/x9.jpg"), 10, 0);
		}
		else{
			graph.drawImage(new Image("../ressources/images/x10.jpg"), 10, 0);
		}
	}
	
	private void goodResult(GameContainer container, int graph) throws SlickException{
		for (int i=0; i<words.size();i++){
			if (words.getCalcul(i).equals(resultat)){
				color.remove(i);
				words.remove(i);
				coordonne.remove(i);
				laser.play();
				int realMultiplicateur = (multiplicateur > 10) ? 10 : multiplicateur;
				score+=realMultiplicateur*100;
				numberGoodResult++;
				upVitesse();
				multiplicateur++;
				if (multiplicateur%5==0) freeze++;
				if (multiplicateur%10==0) bombe++;
			}
		}
	}

	private void badResult(){
		if (!words.contains(resultat) && !resultat.equals("999")){
			multiplicateur=1;
		}
	}

	private void setGameOver(){
		gameOver=true;
	}

	private void stopFreeze(){
		if (time == timeBeforeFreeze+5000 ) freezeBool=false;
	}

	private void upVitesse(){
		if (vitesse <20 && numberGoodResult%5 == 0) {
			vitesse+=1;
		}
		else if(vitesse > 20 && numberGoodResult%6 ==0){
			vitesse+=1;
		}
	}

	private void endGame(GameContainer container){
		for (int i = 0; i < coordonne.size(); i++){
			if (coordonne.get(i)[1]==720) setGameOver();
		}
		if (gameOver == true){
			words.clear();
			coordonne.clear();
			joueur.setScore(score+joueur.getScore());
			container.pause();
		}
	}

}